<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/18 0018
 * Time: 9:51
 */

/**
 * 获取公共的列表状态html
 * @param $id
 * @param $status  1 启用  0 禁用
 * @param $model @要操作的表
 * @return string
 */
function get_list_status_html($status, $id, $model) {
    if ($status == 1) {
        $html = '<a class="label label-success radius" data-title="确定要停用吗？" data-href="' . url('common/save_model_field', ['id' => $id, 'field' => 'status', 'value' => 0, 'model' => $model]) . '">已启用</a>';
    } else {
        $html = '<a class="label label-danger radius" data-title="确定要启用吗？" data-href="' . url('common/save_model_field', ['id' => $id, 'field' => 'status', 'value' => 1, 'model' => $model]) . '">已停用</a>';
    }
    return $html;
}

function get_list_status_2_html($status, $id, $model, $field) {
    if ($status == 1) {
        $html = '<a class="label label-success radius" data-title="确定要停用吗？" data-href="' . url('common/save_model_field', ['id' => $id, 'field' => $field, 'value' => 0, 'model' => $model]) . '">是</a>';
    } else {
        $html = '<a class="label label-danger radius" data-title="确定要启用吗？" data-href="' . url('common/save_model_field', ['id' => $id, 'field' => $field, 'value' => 1, 'model' => $model]) . '">否</a>';
    }
    return $html;
}

/**
 * 是否首页显示
 * @param $id
 * @param $status  1 启用  0 禁用
 * @param $model @要操作的表
 * @return string
 */
function get_list_index_status_html($status, $id, $model) {
    if ($status == 1) {
        $html = '<a class="label label-success radius" data-title="确定要停用吗？" data-href="' . url('common/save_model_field', ['id' => $id, 'field' => 'index', 'value' => 0, 'model' => $model]) . '">已启用</a>';
    } else {
        $html = '<a class="label label-danger radius" data-title="确定要启用吗？" data-href="' . url('common/save_model_field', ['id' => $id, 'field' => 'index', 'value' => 1, 'model' => $model]) . '">已停用</a>';
    }
    return $html;
}

/**
 * 获取设置产品主图的html
 * @param $status
 * @param $id
 * @return string
 */
function get_image_main_html($status, $id) {
    if ($status == 1) {
        $html = '<a class="label label-success radius">是</a>';
    } else {
        $html = '<a class="label label-danger radius" data-title="确定要设为主图吗？" data-href="' . url('set_main', ['id' => $id]) . '">否</a>';
    }
    return $html;
}

/**
 * 获取节点是否在导航显示html
 * @param $status
 * @return string
 */
function get_node_nav_html($status) {
    if ($status == 1) {
        $html = '<a class="label label-success radius">显示</a>';
    } else {
        $html = '<a class="label btn-danger radius">隐藏</a>';
    }
    return $html;
}

/**
 * 获取产品状态html
 * @param $status
 * @param $id
 * @param $model
 * @return string
 */
function get_product_list_status_html($status, $id, $model) {
    if ($status == 1) {
        $html = '<a class="label label-warning  radius" data-title="确定要上架吗？" data-href="' . url('common/save_model_field', ['id' => $id, 'field' => 'status', 'value' => 2, 'model' => $model]) . '">待审核</a>';
    } else if ($status == 2) {
        $html = '<a class="label label-success radius" data-title="确定要下架吗？" data-href="' . url('common/save_model_field', ['id' => $id, 'field' => 'status', 'value' => 3, 'model' => $model]) . '">已上架</a>';
    } else {
        $html = '<a class="label label-danger radius" data-title="确定要上架吗？" data-href="' . url('common/save_model_field', ['id' => $id, 'field' => 'status', 'value' => 2, 'model' => $model]) . '">已下架</a>';
    }
    return $html;
}

/**
 * 获取产品状态html
 * @param $status
 * @return string
 */
function get_pattern_master_list_status_html($status) {
    if ($status == 1) {
        $html = '<a class="label label-warning  radius">待审核</a>';
    } else if ($status == 2) {
        $html = '<a class="label label-success radius">已审核</a>';
    } else if ($status == 3) {
        $html = '<a class="label label-danger radius">已拒绝</a>';
    } else {
        $html = '<a class="label label-danger radius">已关闭</a>';
    }
    return $html;
}

/**
 * 获取订单列表状态html
 * @param $status
 * @return mixed
 */
function get_order_list_status_html($status) {
    $status_array = [
        '',
        '<a class="label label-secondary  radius">待付款</a>',
        '<a class="label label-success  radius">已付款</a>',
        '<a class="label label-warning  radius">量体师已接单</a>',
        '<a class="label label-warning  radius">量体师已确认</a>',
        '<a class="label label-success  radius">已发货</a>',
        '<a class="label label-secondary  radius">已签收</a>',
        '<a class="label label-danger  radius">完成</a>',
        99 => '<a class="label label-danger  radius">物流已签收</a>',
    ];
    return '<p>订单状态 : ' . $status_array[$status] . '</p>';
}

/**
 * 获取订单列表评论状态html
 * @param $status
 * @return mixed
 */
function get_order_list_comment_status_html($status) {
    $status_array = [
        '<a class="label label-secondary  radius">未评论</a>',
        '<a class="label label-warning  radius">已评论</a>',
    ];
    return '<p>评论状态 : ' . $status_array[$status] . '</p>';
}

/**
 * 获取订单列表售后状态html
 * @param $status
 * @return mixed
 */
function get_order_list_after_sale_status_html($status) {
    $status_array = [
        '<a class="label label-secondary  radius">未申请售后</a>',
        '<a class="label label-danger  radius">已申请</a>',
        '<a class="label label-warning  radius">处理中</a>',
        '<a class="label label-success  radius">已完成</a>',
    ];
    return '<p>售后状态 : ' . $status_array[$status] . '</p>';
}

/**
 * 根据pid(上级分类ID)取出后代， 并且给每一代分类定义好级别
 * @param $model
 * @param int $pid
 * @param int $level
 * @param int $noid
 * @return array
 */
function get_cates($model, $pid = 0, $level = 0, $noid = 0)
{
    static $res = [];// static 定义函数内部的全局变量; 当函数内部调用该函数本身时，所得数据需要该定义才能追加保存到同一变量；
    $data = db($model)->where('pid', $pid)->where('delete', 0)->where('status', 1)->where('id', 'neq', $noid)->select();
    foreach($data as $v)
    {
        $v['level'] = $level;	//记录级别
        $res[] = $v;
        get_cates($model, $v['id'], $level+1, $noid);	//函数内部调用该函数本身
    }

    return $res;
}

/**
 * 返回json格式数据
 * @param $code
 * @param array $data
 * @param string $msg
 * @return string
 */
function return_json($code, $data=[], $msg='') {
    return json_encode(['code' => $code, 'data' => $data, 'msg' => $msg]);
}

/**
 * 获取售后状态信息
 * @param $status
 * @return mixed
 */
function get_order_after_sale_status($status) {
    $status_array = [
        '',
        '<a class="label label-secondary  radius">已申请</a>',
        '<a class="label label-success  radius">平台确认</a>',
        '<a class="label label-warning  radius">用户已支付</a>',
        '<a class="label label-warning  radius">用户寄回</a>',
        '<a class="label label-success  radius">平台已收货</a>',
        '<a class="label label-secondary  radius">平台已发货</a>',
        '<a class="label label-danger  radius">用户确认</a>',
    ];
    return $status_array[$status];
}

/**
 * 生成配置文件
 */
function update_config() {
    $config = model('config')->select();
    foreach ($config as $v) {
        $system[$v['name']] = $v['value'];
    }
    file_put_contents('../application/extra/system.php', "<?php\treturn " . var_export($system, true) . ";");
}